package com.jiantong.admin.controller.plantfrom;

import com.jiantong.admin.controller.DjtBaseController;
import com.jiantong.admin.dto.CityAreaDto;
import com.jiantong.admin.model.PagingMz;
import com.jiantong.user.model.BreedFactory;
import com.jiantong.user.model.RolesWeb;
import com.jiantong.user.model.User;
import com.jiantong.user.model.UserProfile;
import com.jiantong.user.service.*;
import com.jiantong.work.model.FeedReport;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 平台管理
 * Created by cao on 2017/7/22.
 */
@Controller
@RequestMapping("/plantform/")
public class PlantformController extends DjtBaseController {
@RpcConsumer
private RolesWebReadService rolesWebReadService;
    @RpcConsumer
    private BreedFactoryReadService breedFactoryReadService;
    @RpcConsumer
    private UserProfileReadService userProfileReadService;
    @RpcConsumer
    private GovernmentReadService governmentReadService;
    @RpcConsumer
    private GovernmentUserDutyReadService governmentUserDutyReadService;
    @RpcConsumer
    private UserReadService userReadService;
    //登录管理
    @GetMapping("loginmgr")
    private  String loginmgr()
    {
        return PFVIEWS+"loginmgr";
    }

    //养殖场
    @GetMapping("breedmgr")
    private String breedmgr()
    {
        return PFVIEWS+"breedmgr";
    }

    //散户
    @GetMapping("breedretailmgr")
    private String breedretailmgr()
    {
        return PFVIEWS+"breedretailmgr";
    }

    //屠宰场
    @GetMapping("botchermgr")
    private String botchermgr()
    {
        return PFVIEWS+"botchermgr";
    }


    //行政区域管理
    @GetMapping("areamgr")
    private String areamgr()
    {
        return PFVIEWS+"areamgr";
    }

    //行政区域管理
    @GetMapping("orgmgr")
    private String orgmgr()
    {
        return PFVIEWS+"orgmgr";
    }

    //权限列表
    @GetMapping("rolemgr")
    private String rolemgr(
            Model model, HttpServletRequest request, HttpServletResponse response)
    {
        Map mapuser =  getarea(response,request);

        model.addAttribute("username",mapuser.get("username"));
        return PFVIEWS+"rolemgr";
    }

    @GetMapping("rolelist")
    @ResponseBody
    private PagingMz<RolesWeb> rolelist(@RequestParam(required = false) String name,@RequestParam(required = false) Integer page,
                                    @RequestParam(required = false) Integer rows
                                    )
    {
        Map map=new HashMap();
        map.put("name",name);
        List<RolesWeb> list= (List<RolesWeb>) rolesWebReadService.findbykeyword(map).getResult();
        Long records=Integer.toUnsignedLong(list.size()); //总记录数
        Long pagetotal = (records-1)/rows+1; //得到总页数
        return new PagingMz<RolesWeb>(pagetotal,records,page,list);
    }



    public Map getarea(HttpServletResponse response, HttpServletRequest request) {
        List<String> list = new ArrayList<>();
        list.add("province");
        list.add("city");
        list.add("region");
        list.add("street");
        list.add("village");
        Cookie[] cookies = null;
        cookies = request.getCookies();
        String name = "";
        Map map = new HashMap();
        for (Cookie cookie : cookies) {

            if (cookie.getName().equals("djtid")) {
                String id = cookie.getValue();
                int Id = Integer.parseInt(id);
                Response<User> userResponse = userReadService.findById(Integer.toUnsignedLong(Id));
                name = userResponse.getResult().getName();
                map.put("username", name);
                switch (userResponse.getResult().getAreaLevel()) {
                    case 1:
                        UserProfile userProfile = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String Province = userProfile.getProvince();
                        Map map6=new HashMap();
                        map6.put("province",Province);
                        List<BreedFactory> breedFactoryList= (List<BreedFactory>) breedFactoryReadService.findByArea(map6).getResult();
                        //获取所有省下城市

                        Map map1 = new HashMap();
                        map1.put("area1",list.get(1));
                        map1.put("area2", list.get(0));
                        map1.put("area3",Province);
                        List<String> citys = breedFactoryReadService.serachAllregion1(map1);
                        //获取城市下所有区
                        List<CityAreaDto> cityAreaDtoList=new ArrayList<>();
                        for (String city : citys) {
                            CityAreaDto cityAreaDto = new CityAreaDto();
                            Map map2 = new HashMap();
                            map2.put("area1", list.get(2));
                            map2.put("area2", list.get(1));
                            map2.put("area3", city);
                            List<String> regions = breedFactoryReadService.serachAllregion1(map2);
                            cityAreaDto.setCity(city);
                            cityAreaDto.setRegions(regions);
                            cityAreaDtoList.add(cityAreaDto);

                        }
                        map.put("cityAreaDtoList",cityAreaDtoList);
                        map.put("breedFactorys",breedFactoryList);
                        //switch break;
                        break;
                    case 2:
                        UserProfile userProfile2 = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String city1 = userProfile2.getCity();
                        Map map7=new HashMap();
                        map7.put("city",city1);
                        List<BreedFactory> breedFactoryList1= (List<BreedFactory>) breedFactoryReadService.findByArea(map7).getResult();
                        //获取所有省下城市

                        Map map21 = new HashMap();
                        map21.put("area1",list.get(2));
                        map21.put("area2", list.get(1));
                        map21.put("area3",city1);
                        List<String> citys2 = breedFactoryReadService.serachAllregion1(map21);
                        //获取城市下所有区
                        List<CityAreaDto> cityAreaDtoList1=new ArrayList<>();
                        for (String city : citys2) {
                            CityAreaDto cityAreaDto = new CityAreaDto();
                            Map map2 = new HashMap();
                            map2.put("area1", list.get(3));
                            map2.put("area2", list.get(2));
                            map2.put("area3", city);
                            List<String> regions = breedFactoryReadService.serachAllregion1(map2);
                            cityAreaDto.setCity(city);
                            cityAreaDto.setRegions(regions);
                            cityAreaDtoList1.add(cityAreaDto);

                        }
                        map.put("cityAreaDtoList",cityAreaDtoList1);
                        map.put("breedFactorys",breedFactoryList1);
                        //switch break;
                        break;
                    case 3://区
                        UserProfile userProfile3 = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String region = userProfile3.getRegion();
                        Map map36=new HashMap();
                        map36.put("region",region);
                        List<BreedFactory> breedFactoryList3= (List<BreedFactory>) breedFactoryReadService.findByArea(map36).getResult();
                        //获取所有区下城市镇

                        Map map31 = new HashMap();
                        map31.put("area1",list.get(3));
                        map31.put("area2", list.get(2));
                        map31.put("area3",region);
                        List<String> citys3 = breedFactoryReadService.serachAllregion1(map31);
                        //获取对应镇 对应村
                        List<CityAreaDto> cityAreaDtoList3=new ArrayList<>();
                        for (String city : citys3) {
                            CityAreaDto cityAreaDto = new CityAreaDto();
                            Map map2 = new HashMap();
                            map2.put("area1", list.get(4));
                            map2.put("area2", list.get(3));
                            map2.put("area3", city);
                            List<String> regions = breedFactoryReadService.serachAllregion1(map2);
                            cityAreaDto.setCity(city);
                            cityAreaDto.setRegions(regions);
                            cityAreaDtoList3.add(cityAreaDto);

                        }
                        map.put("cityAreaDtoList",cityAreaDtoList3);
                        map.put("breedFactorys",breedFactoryList3);
                        //switch break;
                        break;
                    case 4://镇
                        UserProfile userProfile4 = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String street = userProfile4.getStreet();
                        Map map46=new HashMap();
                        map46.put("street",street);
                        List<BreedFactory> breedFactoryList4= (List<BreedFactory>) breedFactoryReadService.findByArea(map46).getResult();
                        //获取所有镇下所有村

                        Map map41 = new HashMap();
                        map41.put("area1", list.get(4));
                        map41.put("area2",  list.get(3));
                        map41.put("area3",street);
                        List<String> citys4 = breedFactoryReadService.serachAllregion1(map41);//所有村

                        List<CityAreaDto> cityAreaDtoList4=new ArrayList<>();

                        CityAreaDto cityAreaDto = new CityAreaDto();

                        cityAreaDto.setCity(street);
                        cityAreaDto.setRegions(citys4);
                        cityAreaDtoList4.add(cityAreaDto);


                        map.put("cityAreaDtoList",cityAreaDtoList4);
                        map.put("breedFactorys",breedFactoryList4);
                        //switch break;
                        break;
                    case 5://村
                        UserProfile userProfile5 = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String village = userProfile5.getVillage();
                        Map map56=new HashMap();
                        map56.put("village",village);
                        List<BreedFactory> breedFactoryList5= (List<BreedFactory>) breedFactoryReadService.findByArea(map56).getResult();

                        ArrayList<CityAreaDto> cityAreaDtoList5=new ArrayList<>();

                        CityAreaDto cityAreaDto1 = new CityAreaDto();
                        List<String> villages=new ArrayList<>();
                        villages.add(village);
                        cityAreaDto1.setCity(village);
                        cityAreaDto1.setRegions(villages);

                        cityAreaDtoList5.add(cityAreaDto1);


                        map.put("cityAreaDtoList",cityAreaDtoList5);
                        map.put("breedFactorys",breedFactoryList5);
                        //switch break;
                        break;
                }


                break;
            }

        }

        return map;
    }





    public List findFactoryIdByArea(  String region, int breedType){

        Map map =new HashMap();
        if (breedType==-1){
            map.put("breedType",null);
        }else{
            map.put("breedType",breedType);
        }
        List<String> list1= new ArrayList<>();
        String [] list =region.split(",");

        for (int i=0;i<list.length;i++){
            list1.add(list[i]);
        }
        map.put("areas",list1);

        List<BreedFactory> breedFactoryList= (List<BreedFactory>) breedFactoryReadService.findByArea(map).getResult();

        return breedFactoryList;
    }

}
